<?php
class DefaultController extends ToolsBaseController
{
	public function actionIndex()
	{
		if (Yii::app()->user->isGuest) {
			$this->redirect(Yii::app()->getModule('tools')->user->loginUrl);
		} else {
			$this->redirect(Yii::app()->createUrl('/tools/merchant'));
		}
	}

	public function actionLogin()
	{
		$model = new ToolsLoginForm('login');

		// if it is ajax validation request
		if (isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if (isset($_POST['ToolsLoginForm'])) {
			$model->attributes = $_POST['ToolsLoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($model->validate() && $model->login()) {
				$url = Yii::app()->createUrl('/tools');
				$this->redirect($url, true);
			} else {
				//throw new CHttpException(500, print_r($model->getErrors(), 1));
			}
		}
		
		// display the login form
		$this->render('login', array('model'=>$model));
	}

	public function actionLogout()
	{
		Yii::app()->user->logout(false);
        $this->redirect(Yii::app()->getModule('tools')->user->loginUrl);
	}
	
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		$error = Yii::app()->errorHandler->error;
		$this->render('error', array('error'=>$error));
	}
}